package com.poker.duiued;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TableRow;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.poker.iuious.yhyhjs.jklimj.User;
import com.poker.jkiuus.R;
import com.poker.jkiuus.uyrfgd.AppManager;
import com.poker.jkiuus.uyrfgd.DatabaseHelper;
import com.poker.jkiuus.uyrfgd.FlagHolder;


public class ClientSettings extends AppCompatActivity {
    private TableRow clear_info;
    private TableRow clear_history;
    private DatabaseHelper databaseHelper;
    private String userName;
    private int userId = -1;
    private Toast currentToast;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.client_settings);

        AppManager.addActivity(this);

        databaseHelper = new DatabaseHelper(this);

        if (getIntent().hasExtra("USER_NAME")){
            userName = getIntent().getStringExtra("USER_NAME");
            if (!userName.isEmpty()){
                User user = databaseHelper.queryUserByNameOrEmail(userName);
                if (user!=null){
                    userId = user.getId();
                }
            }else {
                userName = "";
            }
        }else {
            userName = "";
        }

        clear_info = findViewById(R.id.clear_info);
        clear_info.setOnClickListener(v -> {
            if ("".equals(userName)){
                showRegistrationAlertDialog();
            }else {
                showClearAlertDialog("Clear the conversation cache","Note that this will erase all your conversations with the bot, so think carefully!");
            }
        });

        clear_history = findViewById(R.id.clear_history);
        clear_history.setOnClickListener(v -> {
            if ("".equals(userName)){
                showRegistrationAlertDialog();
            }else {
                showClearAlertDialog("Clear usage records","Please note that this will erase all your usage history of this software, so please consider it carefully!");
            }
        });
    }

    private void showClearAlertDialog(String title,String message) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(title)
                .setMessage(message)
                .setPositiveButton("Know", (dialog, which) -> {
                    clearData(title);
                    dialog.dismiss();
                }).setNegativeButton("Cancel", (dialog, which) -> dialog.dismiss()).setCancelable(false)
                .show();
    }

    private void clearData(String dataType) {
        if (dataType.equals("Clear the conversation cache")) {

            if (userId != -1) {
                databaseHelper.clearChatHistory(String.valueOf(userId));
                FlagHolder.setFlag(true);
                if (currentToast != null) {
                    currentToast.cancel();
                }

                currentToast = Toast.makeText(this, "The conversation history has been cleared", Toast.LENGTH_SHORT);
                currentToast.show();
            }
        } else if (dataType.equals("Clear usage records")) {

            if (userId != -1) {
                databaseHelper.clearUserQueries(userId);
                if (currentToast != null) {
                    currentToast.cancel();
                }

                currentToast = Toast.makeText(this, "Usage records have been cleared", Toast.LENGTH_SHORT);
                currentToast.show();
            }
        }
    }

    private void showRegistrationAlertDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Login is required")
                .setMessage("You don't have an account, please login first.")
                .setPositiveButton("Login", (dialog, which) -> {
                    dialog.dismiss();
                    Intent intent = new Intent(ClientSettings.this, LoginActivity.class);
                    startActivity(intent);
                    AppManager.finishAll();
                }).setNegativeButton("Cancel", (dialog, which) -> dialog.dismiss()).setCancelable(false)
                .show();
    }
}
